package com.guoli.print;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.TreeMap;

import javax.imageio.ImageIO;

public class TestPrintI {
	
	public static void main(String[] args) throws IOException {
		String[] texts = new String[]{"型zyZw","数量"};
		String fontName = "黑体";
		int fontSize = 50;
		String strs[] = getPicString(texts, fontName, fontSize);
		for(int i=0; i<texts.length; i++){
			int[] tlb = getTLBytes(texts[i], fontName, fontSize, 8);
			printZPL(strs[i], tlb);
		}
	}
	
	public static void printZPL(String picString, int[] tlBytes) throws IOException{
//		FileOutputStream fos = new FileOutputStream(new File("/dev/usb/lp0"));
//		String zpl1 ="﻿CT~~CD,~CC^~CT~" +
//				"^XA~TA000~JSN^LT0^MNW^MTT^PON^PMN^LH0,0^JMA^PR5,5~SD15^JUS^LRN^CI0^XZ" + 
//				"^XA~DGOUTSTR01,"+tlBytes[0]+","+tlBytes[1]+"," +
//				picString +
//				"^FO350,100^XGOUTSTR01,1,1,^FS^XZ";
//		fos.write(zpl1.getBytes());	
//		fos.flush();
//		fos.close();
	}
	
	public static int[] getTLBytes(String text, String fontName, int fontSize, int printerDpm){
		Font font = new Font(fontName, Font.BOLD, fontSize);
		int[] tlBytes = new int[2];
		Graphics g = new BufferedImage(1, 1, 1).getGraphics();
		FontMetrics fm = g.getFontMetrics(font);
		int width = (int)Math.round(fm.getStringBounds(text, g).getWidth());
		width = width%8!=0?(width/8+1)*8:width;
		int height = fm.getHeight();
		height = height%8!=0?(height/8+1)*8:height;
		tlBytes[0] = (width*height)/8;
		tlBytes[1] = width/8;
		System.out.println(Arrays.toString(tlBytes));
		return tlBytes;
	}
	
	public static String[] getPicString(String[] text, String fontName, int fontSize) throws IOException{
		int width = 0;
		int height = 0;
		int ascent = 0;
		String[] picString = new String[text.length];
		Font font = new Font(fontName, Font.BOLD, fontSize);
		for(int i=0;i<text.length;i++){
			Graphics g = new BufferedImage(1, 1, 1).getGraphics();
			FontMetrics fm = g.getFontMetrics(font);
			ascent = fm.getAscent();
			width = (int)Math.round(fm.getStringBounds(text[i], g).getWidth());
			width = width%8!=0?(width/8+1)*8:width;
			height = fm.getHeight();
			height = height%8!=0?(height/8+1)*8:height;
			BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
			Graphics2D graphics2d = (Graphics2D) bi.getGraphics();
			graphics2d.setColor(Color.WHITE);
			graphics2d.fillRect(0, 0, width, height);
			graphics2d.setColor(Color.BLACK);
			graphics2d.setFont(font);
			graphics2d.drawString(text[i], 0, ascent);
			FileOutputStream fos = new FileOutputStream(new File("C:\\Documents and Settings\\zhuxiaodong\\桌面\\"+i+".jpg"));
			ImageIO.write(bi, "jpg", fos);
			String strx = "";
			String result = "";
			for(int h = 0; h < height; h++){
				for(int w = 0; w < width; w++){
					strx += new Color(bi.getRGB(w, h)).getGreen()==0 ? "1" : "0";
					if((w+1)%4 == 0){
						result += Integer.toHexString(Integer.parseInt(strx, 2));
						strx = "";
					}
				}
			}
			picString[i] = result.toUpperCase();
			System.out.println(result.toUpperCase());
		}
		return picString;
	}
	
	public static String compressStr(String str){
		Map<Integer, String> m1 = new TreeMap<Integer, String>();
		char c = 'G';
		for(int i = 1; i<20; i++){
			m1.put(i, String.valueOf(c++));
		}
		System.out.println(m1);
		
		return null;
	}
}
